1750C - Complementary XOR - CodeForces Solution


constructive algorithms data structures

Please click on ads to support us..

Python Code:

test=int(input())
while test:
    n=int(input())
    s1=input()
    s2=input()
    all_equal=0
    for i,j in zip(s1,s2):
        if(i==j):
            all_equal+=1
    if(all_equal==0 or all_equal==n):
        print("YES")
        ones=s1.count('1')
        if((all_equal==n and ones%2!=0) or (all_equal==0 and ones%2==0)):
            print(ones+3)
        else:
            print(ones)
        for i in range(n):
            if(s1[i]=='1'):
                print(i+1,i+1)
        if((all_equal==n and ones%2!=0) or (all_equal==0 and ones%2==0)):
            print(1,1)
            print(2,n)
            print(1,n)
    else:
        print("NO")
    test-=1

C++ Code:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define mod 1000000007
#define int long long


void solve(){
    int n; cin>>n;
    string a, b; cin>>a>>b;

    int diff = abs(a[0] - b[0]);
    for(int i = 0; i < n; i++){
        if(abs(a[i] - b[i]) != diff){
            cout<<"NO"<<endl;
            return;
        }
    }

    cout<<"YES"<<endl;

    int aones = 0, azeros = 0, bzeros = 0, bones = 0;
    for(auto &x : a){
        if(x == '1') aones++;
        else azeros++;
    }

    for(auto &x : b){
        if(x == '1') bones++;
        else bzeros++;
    }

    if(bones == n){
        if(aones == n){
            cout<<"2"<<endl;
            cout<<1<<" "<<1<<endl;
            cout<<2<<" "<<n<<endl;
            return;
        }
        else{
            cout<<"3"<<endl;
            cout<<1<<" "<<n<<endl;
            cout<<1<<" "<<1<<endl;
            cout<<2<<" "<<n<<endl;
            return;
        }
    }

    if(bones == 0){
        if(aones == 0){
            cout<<"0"<<endl;
            return;
        }
        else{
            cout<<"1"<<endl;
            cout<<1<<" "<<n<<endl;
            return;
        }
    }


    vector<int> zeros;
    for(int i = 0; i < n; i++) if(a[i] == '0') zeros.push_back(i + 1);

    azeros = azeros - ((a[0] == '0') ? 1 : 0);
    // cout<<azeros<<" ->>>> "<<endl;
    if(azeros % 2 == 0){
        // all b toggles acc to b[0].
        if(b[0] == '0'){
            //all b becomes 0.
            cout<<zeros.size() + 1<<endl;
            for(auto &x : zeros) cout<<x<<" "<<x<<endl;
            cout<<1<<" "<<n<<endl;
        }
        else{
            //all b becomes 1.
            cout<<zeros.size() + 2<<endl;
            for(auto &x : zeros) cout<<x<<" "<<x<<endl;
            cout<<1<<" "<<1<<endl;
            cout<<2<<" "<<n<<endl;
        }
    }
    else{
        if(b[0] == '0'){
            //all b becomes 1.
            cout<<zeros.size() + 2<<endl;
            for(auto &x : zeros) cout<<x<<" "<<x<<endl;
            cout<<1<<" "<<1<<endl;
            cout<<2<<" "<<n<<endl;
        }
        else{
            //all b becomes 0.
            cout<<zeros.size() + 1<<endl;
            for(auto &x : zeros) cout<<x<<" "<<x<<endl;
            cout<<1<<" "<<n<<endl;
        }
    }
}



int32_t main (){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
    int t = 1;
    cin>>t;
    while(t--){
        solve();
    }
return 0;
}


Comments

Submit
0 Comments
More Questions

302A - Eugeny and Array
1638B - Odd Swap Sort
1370C - Number Game
1206B - Make Product Equal One
131A - cAPS lOCK
1635A - Min Or Sum
474A - Keyboard
1343A - Candies
1343C - Alternating Subsequence
1325A - EhAb AnD gCd
746A - Compote
318A - Even Odds
550B - Preparing Olympiad
939B - Hamster Farm
732A - Buy a Shovel
1220C - Substring Game in the Lesson
452A - Eevee
1647B - Madoka and the Elegant Gift
1408A - Circle Coloring
766B - Mahmoud and a Triangle
1618C - Paint the Array
469A - I Wanna Be the Guy
1294A - Collecting Coins
1227A - Math Problem
349A - Cinema Line
47A - Triangular numbers
1516B - AGAGA XOOORRR
1515A - Phoenix and Gold
1515B - Phoenix and Puzzle
155A - I_love_username